<?php
namespace app\common\model\power;
use think\Model;
use think\Db;
use app\common\model\user\User;

class Rolepower extends Model
{
     // 设置当前模型对应的完整数据表名称
        protected $table = 'rolepower';
        protected $pk = 'PowerCode';
        const STATEOK  = 1;
        const STATEINT = 1;


    public static function getpowercode(){
        $userinfo = User::getinfo();
        $role = $userinfo->Role;
        $PowerCode = Rolepower::where('RoleMap','like','%'.$role.'%')->where('State',Rolepower::STATEOK)->column('PowerCode');
        return $PowerCode;
    }

    /**
     * 通过权限id判断用户是否有此权限
     * @param  [type] $PowerCode [description]
     * @return [type]            [description]
     */
    public static function ispower($PowerCode,$userid = ''){
    	$data = RolePower::where('PowerCode',$PowerCode)->where('State',Rolepower::STATEOK)->find();
    	if(empty($data)){
    		return false;
    	}
    	$userinfo = User::getinfo('',$userid);
    	$str = (String)$data->RoleMap;
    	$role = (String)$userinfo->Role;
    	return strpos($str,$role) !== false;
    }
    /**
     * 权限士兵
     * 不符合要求直接拦截
     * @param  [type] $code [description]
     * @return [type]       [description]
     */
    public static function powerguard($code,$code2=''){
        if(Rolepower::ispower($code)){
            return true;
        }
        if(!empty($code2)){
            if(Rolepower::ispower($code2)){
                return true;
            }
        }
        return moerr('没有权限','','gohome');
    }
}
